package com.vcredit.creditcat.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.vcredit.creditcat.dto.creditrepository.AuthItemDto;
import com.vcredit.creditcat.dto.product.CreditCardDto;
import com.vcredit.creditcat.dto.product.MerchantProductDto;
import com.vcredit.creditcat.model.AuthItem;
import com.vcredit.creditcat.model.AuthProccess;
import com.vcredit.creditcat.model.CustProductApply;
import com.vcredit.creditcat.model.CustProductItem;
import com.vcredit.creditcat.model.MerchantProduct;
import com.vcredit.creditcat.model.ProductItem;
import com.vcredit.creditcat.model.RecommendCard;
import com.vcredit.framework.util.BeanUtil;

@Service
public class CustProductService {
	@Autowired
	private MerchantProductService merchantProductService;

	@Autowired
	private CustProductApplyService custProductApplyService;

	@Autowired
	private AuthProccessService authProccessService;

	@Autowired
	private ProductItemService productItemService;

	@Autowired
	private AuthItemService authItemService;

	@Autowired
	private RecommendCardService recommendCardService;

	public List<MerchantProductDto> queryProductDto(Long processId) {
		return findMerchantProduct(merchantProductService.findByProcessId(processId));
	}

	public List<MerchantProductDto> queryProductAuth(Long processId, Boolean isAuth) {
		return findMerchantProduct(merchantProductService.findIsAuthByProcessId(processId, isAuth));
	}

	public List<MerchantProductDto> queryHotProduct(Long processId, Boolean isHot) {
		List<MerchantProductDto> sourceProducts = this.queryProductDto(processId);
		List<MerchantProductDto> merchantProducts = new ArrayList<MerchantProductDto>();
		for (MerchantProductDto merchantProduct : sourceProducts) {
			if (merchantProduct.getIsHot() == isHot) {
				merchantProducts.add(merchantProduct);
			}
		}
		return merchantProducts;
	}

	private List<MerchantProductDto> findMerchantProduct(List<CustProductItem> custProducts) {
		List<MerchantProductDto> merchantProducts = new ArrayList<MerchantProductDto>();
		for (CustProductItem product : custProducts) {
			MerchantProduct merchant = merchantProductService.findById(product.getProductId());
			MerchantProductDto productDto = new MerchantProductDto();
			if (merchant != null) {
				BeanUtil.copy(merchant, productDto);
				merchantProducts.add(productDto);
			}
		}
		return merchantProducts;
	}

	public List<MerchantProductDto> queryApplyProduct(Long custId) {
		List<CustProductApply> custProductApplys = custProductApplyService.findByCustId(custId);
		List<MerchantProductDto> merchantProducts = new ArrayList<MerchantProductDto>();
		for (CustProductApply custProductApply : custProductApplys) {
			MerchantProductDto productDto = new MerchantProductDto();
			MerchantProduct merchant = merchantProductService.findById(custProductApply.getProductId());
			if (custProductApply != null) {
				BeanUtil.copy(custProductApply, productDto);
				BeanUtil.copy(merchant, productDto);
				merchantProducts.add(productDto);
			}
		}
		return merchantProducts;
	}

	public MerchantProductDto queryProductById(Long productId, Long custId) {
		MerchantProduct product = merchantProductService.findById(productId);
		CustProductApply apply = custProductApplyService.findNewApplyProduct(productId, custId);
		MerchantProductDto dto = new MerchantProductDto();
		if (product != null) {
			BeanUtil.copy(product, dto);
			if (apply == null)// 未认证
				dto.setAuditResult(3);
			else
				BeanUtil.copy(apply, dto);
		}
		return dto;
	}

	public List<AuthItemDto> queryProductItem(Long processId, Long productId) {

		List<AuthItemDto> dto = new ArrayList<AuthItemDto>();
		AuthProccess authProccess = authProccessService.findByProcessId(processId);
		ProductItem productItem = productItemService.findByProductId(productId);
		// 产品没有认证的认证项
		List<AuthItem> authFalse = authItemService
				.getItemByCodes(productItem.getMinCode() - (productItem.getMinCode() & authProccess.getSuccessCode()));
		// 已经认证的产品项
		List<AuthItem> authTrue = authItemService
				.getItemByCodes(productItem.getMinCode() & authProccess.getSuccessCode());

		for (AuthItem authItem : authTrue) {
			dto.add(new AuthItemDto(authItem.getItemName(), authItem.getItemDesc(), authItem.getItemIcon(), 1,
					authItem.getItemId().toString(), authItem.getItemBasescore(), authItem.getItemSort()));
		}
		for (AuthItem authItem : authFalse) {
			dto.add(new AuthItemDto(authItem.getItemName(), authItem.getItemDesc(), authItem.getItemIcon(), 0,
					authItem.getItemId().toString(), authItem.getItemBasescore(), authItem.getItemSort()));
		}
		return dto;
	}

	public List<AuthItemDto> queryProductItem(Long processId, Long productId, Boolean isAuth) {
		List<AuthItemDto> dto = new ArrayList<AuthItemDto>();
		AuthProccess authProccess = authProccessService.findByProcessId(processId);
		ProductItem productItem = productItemService.findByProductId(productId);
		List<AuthItem> auth = null;
		// 产品没有认证的认证项
		if (isAuth) {
			// 已经认证的产品项
			auth = authItemService.getItemByCodes(productItem.getMinCode() & authProccess.getSuccessCode());
			for (AuthItem authItem : auth) {
				dto.add(new AuthItemDto(authItem.getItemName(), authItem.getItemDesc(), authItem.getItemIcon(), 1,
						authItem.getItemId().toString(), authItem.getItemBasescore(), authItem.getItemSort()));
			}
		} else {
			// 产品没有认证的认证项
			auth = authItemService.getItemByCodes(
					productItem.getMinCode() - (productItem.getMinCode() & authProccess.getSuccessCode()));
			for (AuthItem authItem : auth) {
				dto.add(new AuthItemDto(authItem.getItemName(), authItem.getItemDesc(), authItem.getItemIcon(), 0,
						authItem.getItemId().toString(), authItem.getItemBasescore(), authItem.getItemSort()));
			}
		}
		return dto;
	}

	public List<MerchantProductDto> queryHotProduct(Boolean isHot) {
		List<MerchantProduct> sourceProducts = merchantProductService.findHotProduct(isHot);
		if (sourceProducts != null && sourceProducts.size() > 0) {
			return BeanUtil.copy(sourceProducts, MerchantProductDto.class);
		}
		return null;
	}

	public List<MerchantProductDto> queryTechionalProduct(Boolean isTechional) {
		List<MerchantProduct> sourceProducts = merchantProductService.findTechnialProduct(isTechional);
		if (sourceProducts != null && sourceProducts.size() > 0) {
			return BeanUtil.copy(sourceProducts, MerchantProductDto.class);
		}
		return null;
	}

	public List<CreditCardDto> recommendCard(Long custId, int filter) throws Exception {
		List<RecommendCard> recomendCards = null;
		List<CreditCardDto> cards = new ArrayList<CreditCardDto>();
		if (filter == 6) {// 全部
			recomendCards = recommendCardService.queryAll();
		} else {
			recomendCards = recommendCardService.findByType(filter);
		}
		for (RecommendCard card : recomendCards) {
			cards.add(new CreditCardDto(card.getCardIcon(), card.getBank(), card.getCardTitle(), card.getApplyAmount(),
					card.getCardContent()));
		}
		return cards;
	}
}
